<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="线路名称" prop="lrName">
        <el-input
          v-model="queryParams.lrName"
          placeholder="请输入线路名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属数据中心" prop="lrDataCenter">
        <el-input
          v-model="queryParams.lrDataCenter"
          placeholder="请输入所属数据中心"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所含机房" prop="lrMachine">
        <el-input
          v-model="queryParams.lrMachine"
          placeholder="请输入所含机房"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="lrCreater">
        <el-input
          v-model="queryParams.lrCreater"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!--临时按钮 -->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:route:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8"> -->
    <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:route:add']"
          >新增</el-button
        >
      </el-col> -->
    <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:route:edit']"
          >修改</el-button
        >
      </el-col> -->
    <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:route:remove']"
          >删除</el-button
        >
      </el-col> -->
    <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:route:export']"
          >导出</el-button
        >
      </el-col> -->
    <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    <!-- </el-row> -->

    <el-table
      v-loading="loading"
      :data="routeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="线路名称" align="center" prop="lrName">
        <!-- <template slot-scope="scope">
          <dict-tag
            :options="dict.type.data_center"
            :value="scope.row.lrName"
          />
        </template> -->
      </el-table-column>
      <el-table-column label="所属数据中心" align="center" prop="lrDataCenter">
        <!-- <template slot-scope="scope">
          <dict-tag
            :options="dict.type.new_machine_room"
            :value="scope.row.lrDataCenter"
          />
        </template> -->
      </el-table-column>
      <el-table-column
        label="所含机房"
        align="left"
        prop="lrMachine"
        width="240"
      >
        <template slot-scope="scope">
          <el-tag v-for="line in scope.row.lrMachine" :key="line" type="info">{{
            line
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="lrCreater" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="lrTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lrTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="lrTemp" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleXQ(scope.row)"
            >详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:route:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:route:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡检路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="线路名称" prop="lrName">
          <el-input v-model="form.lrName" placeholder="请输入线路名称" />
        </el-form-item>
        <el-form-item label="所属数据中心" prop="lrDataCenter">
          <el-select v-model="form.lrDataCenter" placeholder="请选择">
            <el-option
              v-for="item in L_dataCenterList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所含机房" prop="lrMachine">
          <!-- <el-select v-model="" placeholder="请输入所含机房">
            <el-option
              v-for="item in L_MachineList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select> -->
          <el-cascader
            v-model="form.lrMachine"
            :options="L_MachineList"
            :props="MachineProps"
            @change="handleMachineChange"
            collapse-tags
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="创建人" prop="lrCreater">
          <el-input v-model="form.lrCreater" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="lrTime">
          <el-date-picker
            clearable
            v-model="form.lrTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="lrTemp">
          <el-input v-model="form.lrTemp" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoute,
  getRoute,
  delRoute,
  addRoute,
  updateRoute,
} from "@/api/md/route";
import { myDataRouterList, Center_TEXT, Machine_options } from "./data";

export default {
  name: "Route",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检路线表格数据
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lrName: null,
        lrDataCenter: null,
        lrMachine: null,
        lrCreater: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lrName: [
          { required: true, message: "线路名称不能为空", trigger: "blur" },
        ],
      },
      // 临时性：数据
      myDataRouterList,
      MachineProps: { multiple: true },
      L_Data: {
        Center_TEXT,
        Machine_options,
      },
    };
  },
  computed: {
    L_dataCenterList() {
      return Object.keys(this.L_Data.Center_TEXT).map((keyName) => {
        return {
          label: this.L_Data.Center_TEXT[keyName],
          value: this.L_Data.Center_TEXT[keyName],
        };
      });
    },
    L_MachineList() {
      return this.L_Data.Machine_options;
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询巡检路线列表 */
    getList() {
      // this.loading = true;
      // listRoute(this.queryParams).then((response) => {
      //   this.routeList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });

      this.routeList = myDataRouterList;
      this.total = this.routeList.length;
      this.loading = false;
    },
    handleMachineChange() {
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lrName: null,
        lrDataCenter: null,
        lrMachine: null,
        lrCreater: null,
        lrTime: null,
        lrTemp: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.lrId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检路线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lrId = row.lrId || this.ids;
      getRoute(lrId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检路线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.lrId != null) {
            updateRoute(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoute(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lrIds = row.lrId || this.ids;
      this.$modal
        .confirm('是否确认删除巡检路线为"' + row.lrName + '"的数据项？')
        .then(function () {
          return delRoute(lrIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/route/export",
        {
          ...this.queryParams,
        },
        `route_${new Date().getTime()}.xlsx`
      );
    },
    handleXQ(routerData) {
      this.$router.push({ name: "patrolRouteDetail", params: { routerData } });
    },
  },
};
</script>
